﻿@model GolfLeague.EditModels.ScoresScorecardEditModel

<script type="text/javascript">
    $(function () {
        $('.PlayerDropdown').change(function () {
            var playerID = $(this).val();
            var playerNum = $(this).attr('id');
            var matchID = $('#MatchID').val();
            playerNum = playerNum.replace('PlayerScores_', '');
            playerNum = playerNum.replace('__PlayerID', '');
            if (playerNum != '') {
                $.ajax({
                    url: '/Score/PlayerHandicap?id=' + playerID,
                    type: 'GET',
                    dataType: 'json',
                    data: 'playerID=' + playerID + '&matchID=' + matchID,
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        var playerHandicap = data.playerHandicap;
                        var handicapElement = document.getElementById('handicap_' + playerNum);
                        handicapElement.innerText = playerHandicap;
                    }
                });
            }
        });
    });

</script>

<fieldset>
    <legend>Scorecard</legend>
    @if (Model.PlayerScores == null)
    {
        <h4>No scorecard exists for this match.</h4>
    }
    else
    {
    <br />
    <table class="formTable">
        <tr>
            <th rowspan="4" valign="bottom">Team</th>
            <th>Holes</th>
        @foreach (var hole in Model.ScoreCard.FrontHoles)
        {
            <th style="text-align:center">@hole.HoleNumber</th>
        }
            <th style="text-align:center">Total</th>
            <th style="text-align:center">Hdc</th>
            <th style="text-align:center">Net</th>
            <th style="text-align:center">Points</th>
@*            <th style="text-align:center">Edit<br />Points</th>
*@        </tr>
        <tr id="row-FrontTeeBox-@Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxID" style="background-color:@Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxName" @if (String.Compare(Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxName, "White", true) != 0)
                                                                                                                                                 {@Html.Raw("style=\"color:White\"")} >
            <td style="white-space:nowrap;">
                @Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxName (@Model.ScoreCard.FrontHoleDetails.TeeBox.Rating/@Model.ScoreCard.FrontHoleDetails.TeeBox.Slope)
            </td>
        @foreach (var holeInfo in Model.ScoreCard.FrontHoleDetails.HoleDetail)
        {
            <td align="center">
                @holeInfo.Yardage
            </td>
        }
            <td align="center">@Model.ScoreCard.FrontHoleDetails.HoleDetail.Sum(y => y.Yardage)</td>
            <td></td>
            <td></td>
            <td></td>
@*            <td></td>
*@        </tr>
        <tr id="row-FrontPar-@Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxID" @if (String.Compare(Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxName, "White", true) != 0)
                                                                                {@Html.Raw("style=\"color:" + @Model.ScoreCard.FrontHoleDetails.TeeBox.TeeBoxName + "\"")} >
            <td>
                Par
            </td>
        @foreach (var holeInfo in Model.ScoreCard.FrontHoleDetails.HoleDetail)
        {
            <td align="center">
                @holeInfo.Par
            </td>
        }
            <td align="center">@Model.ScoreCard.FrontHoleDetails.HoleDetail.Sum(p => p.Par)</td>
            <td></td>
            <td></td>
            <td></td>
@*            <td></td>
*@        </tr>
        <tr>
            <td>Handicap</td>
        @foreach (var item in Model.ScoreCard.FrontHoles)
        {
            <td align="center">
                @item.Handicap
            </td>
        }
            <td></td>
            <td></td>
            <td></td>
            <td></td>
@*            <td></td>
*@        </tr>
@using (Html.BeginForm("ScoresScorecard", "Admin"))
{
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.MatchID)
    @Html.Hidden("PlayerScoresCount", Model.PlayerScores.Count)
    for (var i = 0; i < Model.PlayerScores.Count; i++)
    {
        <tr>
            <td align="center">@Model.PlayerScores[i].TeamID</td>
            <td>
                @Html.DropDownListFor(model => model.PlayerScores[i].PlayerID, new SelectList(Model.PlayerDropdown, "dropdownID", "dropdownText", Model.PlayerScores[i].PlayerID), new { @class = "PlayerDropdown" })
                @Html.HiddenFor(model => model.PlayerScores[i].PlayerMatch.PlayerMatchID)
                @Html.HiddenFor(model => model.PlayerScores[i].PlayerMatch.PlayerID)
                @Html.HiddenFor(model => model.PlayerScores[i].PlayerMatch.MatchID)
                @Html.HiddenFor(model => model.PlayerScores[i].PlayerMatch.OpponentID)
                @Html.HiddenFor(model => model.PlayerScores[i].PlayerMatch.HandicapID)
            </td>
        @for (var j = 0; j < 9; j++)
        {
            <td>
            @Html.HiddenFor(model => model.PlayerScores[i].PlayerStrokes[j].HoleID)
            @Html.TextBoxFor(model => model.PlayerScores[i].PlayerStrokes[j].Strokes, new { @class = "HoleInput", onChange = "SumPlayerStrokes(" + i + ")" })
            </td>
        }
            <td align="center" @Html.Raw("id=\"stroke_Total_" + i + "\"")>@Html.DisplayTextFor(model => model.PlayerScores[i].PlayerMatch.GrossScore)</td>
            <td align="center" @Html.Raw("id=\"handicap_" + i + "\"")>@Html.DisplayTextFor(model => model.PlayerScores[i].MatchHandicap)</td>
            <td align="center" @Html.Raw("id=\"net_Stroke_Total_" + i + "\"")>@Html.DisplayTextFor(model => model.PlayerScores[i].PlayerMatch.NetScore)</td>
            <td align="center" @Html.Raw("id=\"points_" + i + "\"")>@Convert.ToDecimal(Model.PlayerScores[i].PlayerMatch.Points).ToString("0.###")</td>
@*            <td align="center" @Html.Raw("id=\"points_" + i + "\"")>@Html.TextBoxFor(model => model.PlayerScores[i].PlayerMatch.Points, new { @class = "HoleInput", @readonly = "readonly" })</td>
            <td align="center"><input @Html.Raw("id=\"edit_Points_" + i + "\"") class="EditPoints" type="checkbox" name="EditPoints" onclick="PlayerCheckBox_Click(this);" /></td>
*@        </tr>
        if ((i + 1) == (Model.PlayerScores.Count / 2))
        {
            <tr style="height:10px">
                <th colspan="15"></th>
            </tr>
        }
    }
        <tr>
            <td colspan="15" class="formTable"></td>
        </tr>
        <tr>
            <td colspan="10" class="formTable"></td>
            <th colspan="2" align="right">Total Points</th>
            <td colspan="2">
                Team @Model.TeamMatches[0].TeamID
@*                @Html.HiddenFor(model => model.TeamMatches[0].TeamMatchID)
*@            </td>
            <td align="center">@Model.TeamMatches[0].Points.ToString("0.###")</td>
@*            <td align="center">@Html.TextBoxFor(model => model.TeamMatches[0].Points, new { @class = "HoleInput", @readonly = "readonly", id = "TeamPoints_" + Model.TeamMatches[0].TeamID })</td>
            <td align="center"><input @Html.Raw("id=\"edit_Team_Points_" + Model.TeamMatches[0].TeamID + "\"") class="EditTeamPoints" type="checkbox" name="EditTeamPoints" onclick="TeamCheckBox_Click(this);" /></td>
*@        </tr>
        <tr>
            <td colspan="12" class="formTable"></td>
            <td colspan="2">
                Team @Model.TeamMatches[1].TeamID
@*                @Html.HiddenFor(model => model.TeamMatches[1].TeamMatchID)
*@            </td>
            <td align="center">@Model.TeamMatches[1].Points.ToString("0.###")</td>
@*            <td align="center">@Html.TextBoxFor(model => model.TeamMatches[1].Points, new { @class = "HoleInput", @readonly = "readonly", id = "TeamPoints_" + Model.TeamMatches[1].TeamID })</td>
            <td align="center"><input @Html.Raw("id=\"edit_Team_Points_" + Model.TeamMatches[1].TeamID + "\"") class="EditTeamPoints" type="checkbox" name="EditTeamPoints" onclick="TeamCheckBox_Click(this);" /></td>
*@        </tr>
        <tr>
            <td colspan="15" class="formTable"></td>
        </tr>
}
    </table>
    <p>
        <input type="button" value="Save" onclick="SaveScorecard(this)" />
    </p>

    }
    </fieldset>
